/**
 *  Copyright 2011 Pavlov Dm.
 *  Licensed under the MIT License
 */
 
package org.motiv.tests.store;

import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.After;
import org.junit.BeforeClass;
import org.apache.log4j.BasicConfigurator;

import org.motiv.core.Element;
import org.motiv.core.Cache;
import org.motiv.config.CacheConfiguration;
import org.motiv.tests.config.CacheConfigurationFactory;
import org.motiv.store.MemoryStore;
import org.motiv.tests.policy.ElementFactory;
import org.motiv.tests.objects.TestObject;
/**
 * A cache memory store tests.
 * @author Pavlov Dm
 */

 public class MemoryStoreTest{
	/** MemoryStore instance*/
	private static MemoryStore memoryStore;
	/** Cache instance*/
	private static Cache cache;
	@BeforeClass
    public static void setUpBeforeAll(){
		if(memoryStore == null) {
			BasicConfigurator.configure();
			CacheConfiguration config = CacheConfigurationFactory.create();	
			cache = new Cache(config);
			memoryStore = new MemoryStore(cache,null);	
		}
    }
	@Test
	public void check_put_method() {
		final long EXPECTED_OBJECT_ID = 2L;
		final String EXPECTED_OBJECT_NAME = "second";
		memoryStore.put( ElementFactory.create(1L,"first"));
		memoryStore.put(ElementFactory.create(3L,"third"));
		memoryStore.put(ElementFactory.create(2L,"second"));
		Element element = memoryStore.get(EXPECTED_OBJECT_ID);
		TestObject obj = (TestObject) element.getObjectValue();
		assertEquals("Check putted element id",EXPECTED_OBJECT_ID,obj.getId());
		assertEquals("Check putted element name",EXPECTED_OBJECT_NAME,obj.getName());
	}
	@Test
	public void check_remove_method() {
		memoryStore.put( ElementFactory.create(1L,"first"));
		memoryStore.put(ElementFactory.create(3L,"third"));
		memoryStore.put(ElementFactory.create(2L,"second"));
		memoryStore.remove(2L);
		Element element = memoryStore.get(2L);
		assertNull("Check removed element",element);
	}
	@Test
	public void check_containsKey_method() {
		final long EXPECTED_OBJECT_KEY = 3L;
		memoryStore.put( ElementFactory.create(1L,"first"));
		memoryStore.put(ElementFactory.create(3L,"third"));
		memoryStore.put(ElementFactory.create(2L,"second"));
		assertTrue("Check element key",memoryStore.containsKey(EXPECTED_OBJECT_KEY));
	}
	@After
    public void tearDown(){
		if(memoryStore!=null){
			memoryStore.removeAll();
		}
    }
	@AfterClass
    public static void tearDownAfterAll(){
		if(memoryStore!=null){
			memoryStore.dispose();
			memoryStore = null;
		}
		if(cache!=null){
			cache.dispose();
			cache = null;
		}
    }
}